package android.Dominion.Graph;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class GraphManager
{
	private List<GraphNode> graph;
	private GraphNode currentNode;
	private HashMap<Integer, GraphNode> index;

	public GraphManager()
	{
		Init();
	}

	private void Init()
	{
		graph = new ArrayList<GraphNode>();
		GraphNode startNode = new GraphNode();
		startNode.setId(0);
		graph.add(startNode);
		currentNode = startNode;
	}

	public GraphNode GetNextNode()
	{
		GraphNode nextNode = null;

		// check for unprocessed children
		GraphNode tempNode = getCurrentNode().getNextUnprocessedChild();
		if (tempNode != null)
		{
			nextNode = tempNode;
		}
		// check for unprocessed siblings
		// by going back to this nodes parent and checking for unprocessed
		// children
		else if (getCurrentNode().getParent() != null)
		{

			setCurrentNode(getCurrentNode().getParent());
			nextNode = GetNextNode(); // go go recursion!
		}
		// this node has no parent, so is the head node
		// this node has no unprocessed children, so there is no next node.
		else
		{

			return nextNode = null;
		}

		currentNode = nextNode;
		return nextNode;
	}

	public void SetChoice(Choice choiceIn)
	{
		// do stuff to get choice data into the correct node
		GraphNode node = getIndex().get(choiceIn.getForNodeId());
		node.SetInputValue(choiceIn.getInputId(), choiceIn.getInputValue());
	}

	public void setGraph(List<GraphNode> graph)
	{
		this.graph = graph;
	}

	public List<GraphNode> getGraph()
	{
		return graph;
	}

	public void setCurrentNode(GraphNode currentNode)
	{
		this.currentNode = currentNode;
	}

	public GraphNode getCurrentNode()
	{
		return currentNode;
	}

	public void setIndex(HashMap<Integer, GraphNode> index)
	{
		this.index = index;
	}

	public HashMap<Integer, GraphNode> getIndex()
	{
		return index;
	}

}
